using System;
using System.IO;
using System.IO.IsolatedStorage;
using Graphe;

namespace Graphe.Data
{
    public class IsolatedStorageProvider
    {
        private const string _LAST_DIRECTORY_KEY = "LastIndexPath";
        private const string _LAST_COLLATIONS_DIRECTORY = "LastCollationPath";

        #region Singleton

        private static IsolatedStorageProvider _instance;

        public static IsolatedStorageProvider Instance
        {
            get
            {
                if (_instance == null)
                    _instance = new IsolatedStorageProvider();

                return _instance;
            }
        }

        #endregion

        /// <summary>
        /// Loads the settings from isolated storage.
        /// </summary>
        public void LoadSettings()
        {
            try
            {
                using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForDomain())
                {
                    using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Settings.txt", FileMode.Open, storageFile))
                    {
                        using (StreamReader reader = new StreamReader(stream))
                        {
                            string line = null;
                            while ((line = reader.ReadLine()) != null)
                            {
                                if (line == null)
                                    break;

                                int indexOfColon = line.IndexOf(':');

                                string settingName = line.Substring(0, indexOfColon);
                                string settingValue = line.Substring(indexOfColon + 2, line.Length - 2 - indexOfColon);

                                switch (settingName)
                                {
                                    case _LAST_DIRECTORY_KEY:
                                        Settings.Instance.LastIndexPath = settingValue;
                                        break;
                                    case _LAST_COLLATIONS_DIRECTORY:
                                        Settings.Instance.LastCollationPath = settingValue;
                                        break;
                                }

                            }
                            
                        }
                    }
                }
            }
            catch (FileNotFoundException exc)
            {
                //Not going to do anything about this. Can't figure out a way to see if a file is created yet
                //  in isolated storage. Don't see any way to do an exists check...
            }
        }

        /// <summary>
        /// Saves the settings into isolated storage.
        /// </summary>
        /// <param name="settings">The settings to save.</param>
        public void SaveSettings(Settings settings)
        {
            using (IsolatedStorageFile storageFile = IsolatedStorageFile.GetUserStoreForDomain())
            {
                using (IsolatedStorageFileStream stream = new IsolatedStorageFileStream("Settings.txt", System.IO.FileMode.Create, storageFile))
                {
                    using (System.IO.StreamWriter writer = new System.IO.StreamWriter(stream))
                    {
                        writer.WriteLine(_LAST_DIRECTORY_KEY + "::" + settings.LastIndexPath);
                        writer.WriteLine(_LAST_COLLATIONS_DIRECTORY + "::" + settings.LastCollationPath);
                    }
                }
            }
        }
    }
}
